package poradnik.medyczny.Model;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import poradnik.medyczny.DB.DBAdapter;

public class Localization {
	long id;
	boolean isChecked = false; // to determin if was checked in list
	ArrayList<Likehood> symptoms = new ArrayList<Likehood>();
	ArrayList<Likehood> disease = new ArrayList<Likehood>();
	String name;

	public static ArrayList<Localization> LoadEveryCountry(Context context) {
		ArrayList<Localization> list = new ArrayList<Localization>();
		DBAdapter medical_db = new DBAdapter(context);
		medical_db.openDataBase();
		Cursor cursor = medical_db.getAllLocalizations();
		if (cursor != null) {
			while (cursor.moveToNext()) {
				Localization loc = new Localization();
				loc.id = cursor.getLong(cursor.getColumnIndex(DBAdapter.LOCALIZATIONS_ID));
				loc.name = cursor.getString(cursor.getColumnIndex(DBAdapter.LOCALIZATIONS_NAME));
				Cursor diseaseCursor = medical_db.getLocalizationDiseaseByLoc(loc.id);
				if (diseaseCursor != null) {
					while (diseaseCursor.moveToNext()) {
						Likehood likehood = new Likehood();
						likehood.localiztionId = loc.id;
						likehood.likehood = diseaseCursor.getInt(diseaseCursor.getColumnIndex(DBAdapter.LOCALIZATION_DISEASE_LIKEHOODS_LIKEHOOD));
						likehood.diseaseId = diseaseCursor.getLong(diseaseCursor.getColumnIndex(DBAdapter.LOCALIZATION_DISEASE_LIKEHOODS_DISEASE_ID));
						likehood.id = diseaseCursor.getLong(diseaseCursor.getColumnIndex(DBAdapter.LOCALIZATION_DISEASE_LIKEHOODS_ID));
						loc.disease.add(likehood);
					}
					diseaseCursor.close();
				}
				Cursor symptomCursor = medical_db.getSymptomLocalizationByLoc(loc.id);
				if (symptomCursor != null) {
					while (symptomCursor.moveToNext()) {
						Likehood likehood = new Likehood();
						likehood.localiztionId = loc.id;
						likehood.likehood = symptomCursor.getInt(symptomCursor.getColumnIndex(DBAdapter.SYMPTOM_LOCALIZATION_LIKEHOODS_LIKEHOOD));
						likehood.symptomId = symptomCursor.getLong(symptomCursor.getColumnIndex(DBAdapter.SYMPTOM_LOCALIZATION_LIKEHOODS_SYMPTOM_ID));
						likehood.id = symptomCursor.getLong(symptomCursor.getColumnIndex(DBAdapter.SYMPTOM_LOCALIZATION_LIKEHOODS_ID));
						loc.symptoms.add(likehood);
					}
					symptomCursor.close();
				}
				list.add(loc);
			}
			cursor.close();
		}
		medical_db.close();
		return list;
	}

	public boolean Save(Context context) {
		// does not save LIKEHOOD!
		DBAdapter medical_db = new DBAdapter(context);
		medical_db.openDataBase();

		ContentValues values = new ContentValues();
		values.put(DBAdapter.LOCALIZATIONS_NAME, name);

		boolean successful = medical_db.update(DBAdapter.TABLE_LOCALIZATIONS, values, DBAdapter.LOCALIZATIONS_ID + "='" + id + "'", null);
		if (!successful) {
			values.put(DBAdapter.LOCALIZATIONS_ID, id);
			id = medical_db.insert(DBAdapter.TABLE_LOCALIZATIONS, values);
			successful = id == 0 ? false : true;

		}
		medical_db.close();

		return successful;
	}

	public static Localization parseJSON(JSONObject jsonObject) throws JSONException {
		Localization loc = new Localization();
		loc.id = jsonObject.getLong("id");
		loc.name = jsonObject.getString("name");
		return loc;

	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public boolean isChecked() {
		return isChecked;
	}

	public void setChecked(boolean isChecked) {
		this.isChecked = isChecked;
	}

	public ArrayList<Likehood> getSymptoms() {
		return symptoms;
	}

	public void setSymptoms(ArrayList<Likehood> symptoms) {
		this.symptoms = symptoms;
	}

	public ArrayList<Likehood> getDisease() {
		return disease;
	}

	public void setDisease(ArrayList<Likehood> disease) {
		this.disease = disease;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}
